package com.android.takagism.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.takagism.R;

import com.android.takagism.R;

import androidx.appcompat.app.AppCompatActivity;

import com.android.takagism.R;
import com.android.takagism.service.ClientService;
import com.android.takagism.service.ClientThread;
import com.android.takagism.utils.MyContext;

import java.io.IOException;
import java.io.OutputStream;

public class ChooseActivity extends AppCompatActivity implements View.OnClickListener {

    private MyContext myContext = MyContext.getInstance();
    public static Handler mainHandler;
    public static boolean isConnected;
    private Button easy;
    private Button middle;
    private Button hard;
    private Button exit_btn;
    private ClientService clientService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose);
        init();
    }

    private void init() {
        easy = findViewById(R.id.easy_button);
        easy.setOnClickListener(this);
        middle = findViewById(R.id.middle_button);
        middle.setOnClickListener(this);
        hard = findViewById(R.id.hard_button);
        hard.setOnClickListener(this);
        exit_btn = findViewById(R.id.exit_button);
        exit_btn.setOnClickListener(this);
        initMainHandler();
        clientService = new ClientService();
        clientService.doConnect();
        myContext.setStatics("clientService", clientService);
    }


    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.exit_button:
                System.out.println("退出");
                finish();
                break;
            case R.id.easy_button:
                System.out.println("一般");
                myContext.setStatics("hardLevel", 1);
                startActivity(new Intent(ChooseActivity.this, BeforeUserMainActivity.class));
                break;
            case R.id.middle_button:
                System.out.println("中等");
                startActivity(new Intent(ChooseActivity.this, BeforeUserMainActivity.class));
                myContext.setStatics("hardLevel", 2);
                break;
            case R.id.hard_button:
                System.out.println("困难");
                myContext.setStatics("hardLevel", 3);
                startActivity(new Intent(ChooseActivity.this, BeforeUserMainActivity.class));
                break;
            default:
                break;
        }
    }

    @SuppressLint("HandlerLeak")
    void initMainHandler() {

        mainHandler = new Handler() {

            @Override
            public void handleMessage(Message msg) {

                // 接收子线程的消息
                switch (msg.what) {
                    case 0x1111://接收数据
                        System.out.println("----------------接收到服务器发送数据： " + msg.obj.toString());
                        break;
                    case 0x1112://发送数据--未使用
                        Bundle bundle = msg.getData();
                        byte[] buffer = bundle.getByteArray("sendData");
                        OutputStream outputStream = clientService.getOutputStream();
                        try {
                            outputStream.write(buffer);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        break;
                    case 0x5555://socket连接成功
                        isConnected = true;
                        System.out.println("Toast.makeText(AdminActivity.this, 连接成功！,Toast.LENGTH_LONG).show();");
                        Toast.makeText(ChooseActivity.this, "与服务器连接成功！", Toast.LENGTH_LONG).show();
                        sendBroadcast(new Intent("com.android.action.nettestfinished"));
                        break;
                    case 0x5556://socket连接失败
                        isConnected = false;
                        System.out.println("Toast.makeText(AdminActivity.this, 连接失败！,Toast.LENGTH_LONG).show();");
                        @SuppressLint("ShowToast") Toast toast = Toast.makeText(ChooseActivity.this, "失去连接/连接失败！", Toast.LENGTH_LONG);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        UserMainActivity.showMyToast(toast, 3000);

                        finish();
                        break;
                    default:
                        break;
                }

            }

        };


    }
}